package com.xh.core.util;

import com.whalin.MemCached.MemCachedClient;
import com.xh.core.properties.XhProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/*
 * @Author daiwei
 * @Description
 * @Date 2018/9/26 19:09
 **/
@Component
public class CacheUtils {

    private static MemCachedClient cachedClient;
    
    private static RedisTemplate<String, Object> redisTemplate;
    
    private static String cacheType;

    private static String cachePrefixKey;
    
    static {
        if (cachedClient == null)
            cachedClient = new MemCachedClient();
    }

    private CacheUtils() {

    }
    /**
     * 注入静态 redisTemplate
     * @param redisTemplate
     */
    @Resource(name="redisTemplate")
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
    	CacheUtils.redisTemplate = redisTemplate;
    }
    /**
     * 注入静态 cacheType
     * @param xhProperties
     */
    @Resource(name="xhProperties")
    public void setRedisTemplate(XhProperties xhProperties) {
    	CacheUtils.cacheType = xhProperties.getCacheType();
        CacheUtils.cachePrefixKey = xhProperties.getCachePrefixKey();
    }
    
    public static boolean set(String key, Object value){
        try{
            String newKey = String.format("%s_%s", cachePrefixKey, key);
        	if("redis".equals(cacheType)){
        		redisTemplate.opsForValue().set(newKey, value);
        		return true;
        	}else{
        		return cachedClient.set(newKey,value);
        	}
        }catch (Exception ex){
        	ex.printStackTrace();
        }
        return false;
    }

    public static boolean set(String key, Object value, Integer expire){
        try{
            String newKey = String.format("%s_%s", cachePrefixKey, key);
        	if("redis".equals(cacheType)){
        		redisTemplate.opsForValue().set(newKey, value, expire, TimeUnit.SECONDS);
        		return true;
        	}else{
        		return cachedClient.set(newKey,value,new Date(expire));
        	}
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        return false;
    }

    public static boolean delete(String key){
        try{
            String newKey = String.format("%s_%s", cachePrefixKey, key);
        	if("redis".equals(cacheType)){
        		redisTemplate.delete(newKey);
        		return true;
        	}else{
        		return cachedClient.delete(newKey);
        	}
        }catch (Exception ex){

        }
        return false;
    }
    public static Object get(String key){
        try{
            String newKey = String.format("%s_%s", cachePrefixKey, key);
        	if("redis".equals(cacheType)){
        		return redisTemplate.opsForValue().get(newKey);
        	}else{
        		return cachedClient.get(newKey);
        	}
        }catch (Exception ex){

        }
        return null;
    }
}
